package com.tobeface.sns.domain;

import java.util.Collections;
import java.util.List;

import com.google.common.collect.Lists;

/**
 * 
 * @author loudyn
 * 
 */
public final class UserAvatar {

	private final static int DEFAULT_AVATARS_LIMIT = 3;

	private int limit = DEFAULT_AVATARS_LIMIT;
	private List<String> avatars = Lists.newLinkedList();

	/**
	 * 
	 * @return
	 */
	public String getNewest() {
		return avatars.isEmpty() ? null : avatars.get(0);
	}

	/**
	 * 
	 * @param avatar
	 * @return
	 */
	public UserAvatar addNewest(String avatar) {

		addFirstTo(avatar);
		if (isOverLimit()) {
			removeOldest();
		}

		return this;
	}

	private void addFirstTo(String avatar) {
		avatars.add(0, avatar);
	}

	private boolean isOverLimit() {
		return avatars.size() > limit;
	}

	private void removeOldest() {
		avatars.remove(avatars.size() - 1);
	}

	/**
	 * 
	 * @return
	 */
	public UserAvatar clear() {
		avatars.clear();
		return this;
	}

	/**
	 * 
	 * @return
	 */
	public List<String> getAvatars() {
		return Collections.unmodifiableList(avatars);
	}

	UserAvatar() {}
}
